package com.gisuni.sddc.platformManager.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.model.RoleInfo;
import com.gisuni.sddc.platformManager.model.Tree;
import com.gisuni.sddc.platformManager.model.UserInfo;
import com.gisuni.sddc.platformManager.service.RoleService;
import com.gisuni.sddc.platformManager.service.UserService;
import com.gisuni.sddc.util.StringUtil;

@IocBean(singleton = false)
@At("/client/backgroundManagement/userController")
public class UserController {

	private Log log = Logs.getLog(UserController.class);

	@Inject
	private UserService  userService;

	@Inject
	private RoleService  roleService;
	
	UserInfo user = new UserInfo();
	
	
	public void setUser(UserInfo user) {
		this.user = user;
	}
	
	public UserInfo getUser() {
		return user;
	}
	
	/**
	 * 添加
	 * 
	 * @return
	*/ 
	@At("/add")
	@GET
	@POST
	@Ok("json")
	public Object add(@Param("organizationMenagerId") String organizationMenagerId,@Param("organizationName") String organizationName, @Param("organizationType") String organizationType, @Param("parentId") String parentId ,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		try {
			res = userService.addUser(request);
//			JSONObject o = Response.toJSONObject(res);
//			ObjectMapper mapper = new ObjectMapper();
//			r = mapper.readValue(o.toString(), Map.class);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
	}
	
	@At("/editPage")
	@GET
	@POST
	@Ok("jsp:/client/backgroundManagement/views/admin/userEdit.jsp")
    public String editPage(String id, HttpServletRequest request) {
        UserInfo userInfo = userService.findUserInfoById(id);
        List<String> roleIds = new ArrayList<String>();
        List<String> orgIds = new ArrayList<String>();
        if(userInfo!=null){
        	if(!StringUtil.isNullOrEmpty(userInfo.getRoleIds())){
        		
        		 request.setAttribute("roleIds", userInfo.getRoleIds()); //
        		 
                //显示优先级  拓展组-->事业部-->集团
	        	if(!StringUtil.isNullOrEmpty(userInfo.getOrgDevelopId())){
	        		orgIds.add(userInfo.getOrgDevelopId());
	        		request.setAttribute("organizationId",userInfo.getOrgDevelopId() );
	        	}else if(!StringUtil.isNullOrEmpty(userInfo.getOrgBussinessId())){
	        		orgIds.add(userInfo.getOrgBussinessId());
	        		request.setAttribute("organizationId", userInfo.getOrgBussinessId());
	        	}else if(!StringUtil.isNullOrEmpty(userInfo.getOrgCompanyId())){
	        		orgIds.add(userInfo.getOrgCompanyId());
	        		request.setAttribute("organizationId", userInfo.getOrgCompanyId());
	        	} 
        	} 

        }
 
//        System.out.println("roleIds"+userInfo.getRoleIds());
       
        request.setAttribute("user", userInfo);
        return "/client/backgroundManagement/views/admin/userEdit.jsp";
    }
    
	/**
	 * 查询
	 * 
	 * @return
	 */
	@At("/query")
	@GET
	@POST
	@Ok("raw:json") 
	public Object queryBaseUserAll(@Param("name") String userName,@Param("organizationId") String organizationId,@Param("page") int page, @Param("rows") int rows,HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			int from=(page-1)*rows;
			res = userService.queryBaseUserAll(from,from+rows+1,userName,organizationId);
			r = Response.toJSONObject(res);
//			ObjectMapper mapper = new ObjectMapper();
//			r = mapper.readValue(o.toString(), Map.class);
//			System.out.println(r.toString());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return r;
	}
	
	@At("/tree")
	@GET
	@POST
	@Ok("json")
    public Object tree(HttpServletRequest request) {
		List<Tree>  trees =  userService.findTree();
		return trees;
    }
	
	
	/**
	 * 修改数据
	 */
	@At("/edit")
	@GET
	@POST
	@Ok("json")
	public Object update(HttpServletRequest request) {
		
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = userService.updateUser(request);
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg("操作失败");
			return res;
		}
		return res;
	}
	
 
	
	/**
	 * 修改密码数据
	 */
	@At("/updatePwd")
	@GET
	@POST
	@Ok("json")
	public Object updatePwd(HttpServletRequest request) {
		
		Response res = new Response();
		res.setSuccess(false);
		Object r = res;
		try {
			res = userService.updatePwd(request);
		} catch (Exception e) {
			log.error(e.getMessage());
			res.setMsg("操作失败");
			return res;
		}
		return res;
	}
	
	/**
	 * 删除数据
	 * 
	 * @param UserId
	 * @return
	*/
	@At("/delete")
	@GET
	@POST
	@Ok("json")
	public Object delete(@Param("id") String id, HttpServletRequest request) {
		Response res = new Response();
		res.setSuccess(false);
		try {
			res =  userService.deleteUserById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return res;
	}
	


 
 
	

	 
 
 
}
